package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.BargainGroupService;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.req.SyncDomesticDeliveredInfoBean;
import com.ymatou.iapi.trading.operate.service.SyncDomesticDeliveredInfoCall;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.query.seller.trading.api.parameter.GetPackageDeliveryOrdersCountBean;
import com.ymatou.query.seller.trading.api.parameter.resp.GetPackageDeliveryOrdersCountResponse;
import com.ymatou.query.seller.trading.api.parameter.sp.Sp_GetPackageDeliveryOrdersCount;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*查询国内段未发货的拼邮订单接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPackageDeliveryOrdersCount {
	private static GetPackageDeliveryOrdersCountBean request;
	//	private static com.ymatou.query.seller.trading.api.service.GetPackageDeliveryOrdersCountCall call;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetPackageDeliveryOrdersCountCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询国内段未发货的拼邮订单接口");
	}

	@Before
	public void caseUp() {
		request = new GetPackageDeliveryOrdersCountBean();
		//		call = new com.ymatou.query.seller.trading.api.service.GetPackageDeliveryOrdersCountCall();
		call = new com.ymatou.iapi.trading.sellerquery2.service.GetPackageDeliveryOrdersCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings("rawtypes")
	private void ConstructResponse(
			GetPackageDeliveryOrdersCountResponse response) {
		Sp_GetPackageDeliveryOrdersCount sp = new Sp_GetPackageDeliveryOrdersCount();
		sp.setSellerId(request.getSellerId());
		sp.setDomesticDelivered(request.getDomesticDelivered());
		sp.setTimeFrom(request.getTimeFrom());
		sp.setTimeTo(request.getTimeTo());
		List<Map> results = sp.execute();
		if (DataUtil.isNullOrEmpty(results)) {
			response = new GetPackageDeliveryOrdersCountResponse();
		} else {
			for (Map result : results) {
				if ((boolean) result.get("shangou") == true) {
					response.setShangouOrdersCount(DataUtil.Integerr
							.GetValueOrDefault(result.get("count")));
				} else {
					response.setSpotOrdersCount(DataUtil.Integerr
							.GetValueOrDefault(result.get("count")));
				}
			}
		}
		response.setTotalOrdersCount(response.getShangouOrdersCount()
				+ response.getSpotOrdersCount());
	}

	private void verify() {
		GetPackageDeliveryOrdersCountResponse expectResponse = new GetPackageDeliveryOrdersCountResponse();
		ConstructResponse(expectResponse);
		GetPackageDeliveryOrdersCountResponse actualResponse = call
				.getPackageDeliveryOrdersCountResponse();
		DataUtil.verify(expectResponse.getShangouOrdersCount(),
				actualResponse.getShangouOrdersCount(), "验证ShangouOrdersCount");
		DataUtil.verify(expectResponse.getSpotOrdersCount(),
				actualResponse.getSpotOrdersCount(), "验证SpotOrdersCount");
		DataUtil.verify(expectResponse.getTotalOrdersCount(),
				actualResponse.getTotalOrdersCount(), "验证TotalOrdersCount");
	}
	/**
	 * 	发货并同步国内物流
	 * 
	*/
	private void syncDomesticDeliveredInfo(int sellerId, int orderId) {
		//接单
		new OrderCallService().acceptOrder(orderId, sellerId);

		// 商家发货
		new OrderCallService().dispatched(orderId, sellerId);

		SyncDomesticDeliveredInfoBean syncdomesticdeliveredinfoBean = new SyncDomesticDeliveredInfoBean();
		SyncDomesticDeliveredInfoCall syncdomesticdeliveredinfoCall = new SyncDomesticDeliveredInfoCall();

		syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
		syncdomesticdeliveredinfoBean.setOrderId(orderId);
		syncdomesticdeliveredinfoBean.setLogisticsNo("autotest_"
				+ TestDataManager.getRandomIntNum(8));
		syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));

		syncdomesticdeliveredinfoCall.setData(false,
				syncdomesticdeliveredinfoBean);

		try {
			syncdomesticdeliveredinfoCall.callService();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_001() {
		Logger.start(true, "查询国内段未发货的拼邮订单成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			request.setSellerId(SellerId);
			//表示国内物流还没发货
			request.setDomesticDelivered(false);
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5);
			request.setTimeFrom(BeginTime);
			request.setTimeTo(EndTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_002() {
		Logger.start(true, "查询国内段已发货的拼邮订单成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			request.setSellerId(SellerId);
			//表示国内物流已发货
			request.setDomesticDelivered(true);
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5);
			request.setTimeFrom(BeginTime);
			request.setTimeTo(EndTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_003() {
		Logger.start(true, "查询时间范围内的全部拼邮订单成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			request.setSellerId(SellerId);
			request.setDomesticDelivered(null);
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5);
			request.setTimeFrom(BeginTime);
			request.setTimeTo(EndTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_004() {
		Logger.start(true, "查询时间范围内的订单成功, 只timeFrom");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			request.setSellerId(SellerId);
			request.setDomesticDelivered(null);
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			request.setTimeFrom(BeginTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_005() {
		Logger.start(true, "砍价团订单,组团成功,查询时间范围内的订单成功 ");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 砍价团id
			String bizId = placeOrderResp.getBizId();
			// 开团订单
			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 砍价团参团订单
			PlaceOrderResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);
			// 参团
			Integer orderId2 = placeOrderResp2.getOrderList().get(0)
					.getOrderId();

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 1, 3);

			syncDomesticDeliveredInfo(sellerId, orderId);

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setDomesticDelivered(null);

			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			request.setTimeFrom(BeginTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_006() {
		Logger.start(true, "砍价团订单,组团失败,查询时间范围内的订单成功 ");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 砍价团id
			String bizId = placeOrderResp.getBizId();
			// 开团订单
			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 砍价团参团订单
			PlaceOrderResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);
			// 参团
			Integer orderId2 = placeOrderResp2.getOrderList().get(0)
					.getOrderId();

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 1, -3);

			syncDomesticDeliveredInfo(sellerId, orderId);

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setDomesticDelivered(null);

			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			request.setTimeFrom(BeginTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetPackageDeliveryOrdersCount_007() {
		Logger.start(true, "砍价团订单,组团中,查询时间范围内的订单成功 ");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 砍价团id
			String bizId = placeOrderResp.getBizId();
			// 开团订单
			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 砍价团参团订单
			PlaceOrderResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);
			// 参团
			Integer orderId2 = placeOrderResp2.getOrderList().get(0)
					.getOrderId();

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 1, 0);

			syncDomesticDeliveredInfo(sellerId, orderId);

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setDomesticDelivered(null);

			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			request.setTimeFrom(BeginTime);
			// seller 后台发送请求
			String param = FormatUtil.beanToHttpString(request, true, true);
			call.setData(param);
			call.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}